import java.util.Random;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * 获取元素时等待队列变为非空，以及存储元素时等待空间变得可用
 * 
 * @author 10255_000
 *
 */

public class TestBlockingQueue {
	public static void main(String[] args) {
		final BlockingQueue<Integer> queue = new LinkedBlockingQueue<Integer>(3);
		final Random random = new Random();

		class Producer implements Runnable {
			@Override
			public void run() {
				while (true) {
					try {
						int i = random.nextInt(100);
						queue.put(i);// �����дﵽ����ʱ�򣬻��Զ������
						if (queue.size() == 3) {
							System.out.println("full");
						}
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}
		}

		class Consumer implements Runnable {
			@Override
			public void run() {
				while (true) {
					try {
						queue.take();// ������Ϊ��ʱ��Ҳ���Զ�����
						Thread.sleep(1000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}
		}

		new Thread(new Producer()).start();
		new Thread(new Consumer()).start();
	}
}
